import Vue from 'vue'
import Vuex from 'vuex'
import {getTheme,setTheme,getCookie,getUserInfo} from '../utils/localstorage.js'
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		themeType:getTheme()||'light',
		cookie:getCookie(),
		userInfo:getUserInfo(),
	},
	mutations: {
		changeTheme(state){
			if(state.themeType==='light'){
				state.themeType='dark'
				setTheme('dark')
				uni.setTabBarStyle({
					"color": "#fff",
					"selectedColor": "#ca2137",
					"borderStyle": "black",
					"backgroundColor": "#17171a",
				})
				uni.setNavigationBarColor({
					frontColor:'#ffffff',
					backgroundColor:'#000',
				},800)
			}else{
				state.themeType='light'
				setTheme('light')
				uni.setTabBarStyle({
					"color": "#ccc",
					"selectedColor": "#000",
					"borderStyle": "black",
					"backgroundColor": "#fff",
				})
				setTimeout(()=>{
					uni.setNavigationBarColor({
						frontColor:'#000000',
						backgroundColor:'#fff',
					},800)
				})
			}
		},
		clearUserInfo(state){
			uni.removeStorageSync('userInfo')
			uni.removeStorageSync('cookie')
			uni.clearStorageSync()
			state.userInfo=null
			state.cookie=null
		},
		saveLoginInfo(state,resulte){
			state.cookie=resulte.cookie
			state.userInfo=resulte
			uni.setStorageSync('userInfo',resulte)
			uni.setStorageSync('cookie',resulte.cookie)
			uni.switchTab({
				url:'/pages/index/index'
			})
		}
	},
	actions: {
	   logout({commit}){
		   uni.showModal({
		       title: '退出登录',
		       content: '您确定要退出登录吗？',
		       success: function (res) {
		           if (res.confirm) {
		               let cookie = uni.getStorageSync('cookie')
		               uni.request({
		               	url:'https://music-1247192-1307429763.ap-shanghai.run.tcloudbase.com/logout?cookie='+cookie
		               }).then(res=>{
		               	   commit('clearUserInfo')
						   uni.navigateTo({
						       url: '/pages/login/login.vue'
						   });
		               })
		           } else if (res.cancel) {
		               console.log('用户点击取消');
		           }
		       }
		   });
	   }
	}
})

export default store
